package com.wl.controller;

import com.wl.pojo.PageBean;
import com.wl.pojo.Personal.Collect;
import com.wl.pojo.Personal.PersonalUser;
import com.wl.service.PersonalService;
import com.wl.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/personal")
public class PersonalController {

    @Autowired
    private PersonalService personalService;

    /**
     * 获取用户的基本信息，包括兴趣标签
     */
    @GetMapping("/info")
    public Result getPersonalInfo(@RequestParam("personalId") Integer userId) {
        return Result.success(personalService.getPersonalInfo(userId));
    }

    /**
     * 分页获取关注的情况，包括获取粉丝和获取关注的用户两种情况
     * currentSwitch 用来判断是查询关注还是查询粉丝
     */
    @GetMapping("/follow")
    public Result getFollowOrFans(Integer currentSwitch, @RequestParam("personalId") Integer userId,
                                @RequestParam(defaultValue = "1") Integer currentPage,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean = personalService.getFollowOrFans(currentSwitch, userId, currentPage, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 获取当前登陆用户的关注用户的id列表
     */
    @GetMapping("/follow/lg")
    public Result getLoginUserFollow(Integer userId) {
        return Result.success(personalService.getLoginUserFollow(userId));
    }

    /**
     * 获取个人主页页面的展示的文章内容
     */
    @GetMapping("/post")
    public Result getPersonalPost(@RequestParam("activeName") String activeName,
                                 @RequestParam("personalId") Integer userId,
                                 @RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean = personalService.getPersonalPost(activeName, userId, currentPage, pageSize);
        return Result.success(pageBean);
    }

    /**
     *
     * @param userId 用户ID
     * @return 返回 所有收藏夹以及收藏夹中收藏的内容
     */
    @GetMapping("/collect")
    public Result getCollectData(Integer userId) {
        return Result.success(personalService.getCollectData(userId));
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/post/delete")
    public Result deletePostById(Integer postId) {
        personalService.deletePostById(postId);
        return Result.success();
    }

    /**
     * 获取用户可供选择的兴趣标签数据
     */
    @GetMapping("/interest")
    public Result getInterest() {
        return Result.success(personalService.getInterest());
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/user")
    public Result getUserInfo(@RequestParam Integer userId) {
        return Result.success(personalService.getUserInfo(userId));
    }

    /**
     * 修改用户信息，仅包括用户名，个人介绍。兴趣标签
     */
    @PostMapping("/user/update")
    public Result updateUserInfo(@RequestBody PersonalUser personalUser) {
        log.info("修改用户信息:{}", personalUser);
        personalService.updateUserInfo(personalUser);
        return Result.success();
    }

    /**
     * 修改密码接口
     */
    @PostMapping("/user/update/password")
    public Result updatePassword(Integer userId, String password) {
        personalService.updatePassword(userId, password);
        return Result.success();
    }

    /**
     * 删除收藏夹
     * @param categoryId 收藏夹id
     * @return Result 成功返回success，失败返回error
     */
    @DeleteMapping("/collect/delete")
    public Result deleteCategory(@RequestParam Integer categoryId) {
        personalService.deleteCategory(categoryId);
        return Result.success();
    }

    /**
     * 更新收藏夹
     */
    @PostMapping("/collect/update")
    public Result updateCategory(@RequestBody Collect collect) {
        personalService.updateCategory(collect);
        return Result.success();
    }
}
